package com.neu.questionnaireplus.service.impl;

import com.neu.questionnaireplus.dao.AnswererDao;
import com.neu.questionnaireplus.dao.GroupDao;
import com.neu.questionnaireplus.dao.GroupToAnswererDao;
import com.neu.questionnaireplus.entity.Answerer;
import com.neu.questionnaireplus.entity.Group;
import com.neu.questionnaireplus.entity.GroupToAnswerer;
import com.neu.questionnaireplus.service.GroupService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

@Validated
@Service
public class GroupServiceImpl implements GroupService{

    @Autowired
    private GroupDao groupDao;

    @Autowired
    private AnswererDao answererDao;

    @Autowired
    private GroupToAnswererDao groupToAnswererDao;


    @Override
    public Group queryGroupByGroupname(String groupname) {
        return groupDao.queryGroupByGroupname(groupname);
    }

    @Override
    public List<Group> queryGroup() {
        return groupDao.queryGroup();
    }

    @Override
    public List<Group> queryExistGroup() {
        return groupDao.queryExistGroup();
    }

    @Override
    public List<Group> selectGroupList(Group group) {
        return groupDao.selectGroupList(group);
    }

    @Override
    public Group queryGroupByID(String id) {
        return groupDao.queryGroupByID(id);
    }

    @Transactional
    @Override
    public boolean insertGroup(Group group) {
        if (group != null && !group.getId().equals("")){
            try {
                String groupname = group.getGroupname();
                if (groupDao.queryGroupByGroupname(groupname) != null) {
                    throw new RuntimeException("群组名已存在");
                } else {
                    int i = groupDao.insertGroup(group);
                    if (i == 1) return true;
                    else throw new RuntimeException("插入群组失败！！" + group);
                }
            }catch (Exception e){
                throw new RuntimeException("插入群组失败！！" + e.getMessage());
            }
        }else {
            throw new RuntimeException("插入群组失败,群组id不能为空");
        }
    }
    @Transactional
    @Override
    public boolean updateGroup(Group group) {
        if (group != null && !group.getId().equals("")){
            try {
                String groupname = group.getGroupname();
                Group group1 = groupDao.queryGroupByGroupname(groupname);
                if ((group1 != null) && ( (group1 != null && !group.getId().equals(group1.getId())) ) ){
                    throw new RuntimeException("修改的用户名或手机号与其他用户有冲突");
                }else {
                    int i = groupDao.updateGroup(group);
                    if (i == 1) return true;
                    else throw new RuntimeException("更新群组失败！！" + group);
                }
            }catch (Exception e){
                throw new RuntimeException("更新群组失败！！" + e.getMessage());
            }
        }else {
            throw new RuntimeException("更新群组失败,群组id不能为空");
        }
    }

    @Transactional
    @Override
    public boolean deleteGroup(String id) {
        if (id != null && !id.equals("")){
            try {
                int i = groupDao.deleteGroup(id);
                if (i == 1) return true;
                else throw new RuntimeException("删除群组失败！！" + id);
            }catch (Exception e){
                throw new RuntimeException("删除群组失败！！" + e.getMessage());
            }
        }else {
            throw new RuntimeException("删除群组失败,群组id不能为空");
        }
    }

    @Override
    public boolean hasUserGroup(String id) {
        List<GroupToAnswerer> groupToAnswerers = groupToAnswererDao.queryRelationshipByGroupId(id);
        for (GroupToAnswerer groupToAnswerer : groupToAnswerers){
            Answerer answerer = answererDao.queryAnswererByID(groupToAnswerer.getAnswererId());
            if (answerer != null && answerer.getDelFlag() != 1){
                return true;
            }
        }
        return false;
    }

    @Override
    public List<Group> queryGroupByName(String groupname) {
        return groupDao.queryGroupByName(groupname);
    }

    @Transactional
    @Override
    public boolean modifyGroupInfo(Group group) {
        String id = group.getId();
        if (groupDao.queryGroupByID(id) != null){
            if (hasUserGroup(id)){
                throw new RuntimeException("该群组中存在用户，不可修改信息");
            }else {
                int flag = groupDao.updateGroup(group);
                if (flag == 1){
                    return true;
                }else {
                    throw new RuntimeException("修改失败");
                }
            }
        }else {
            throw new RuntimeException("群组不存在");
        }
    }

    @Transactional
    @Override
    public boolean deleteGroupInfo(Group group) {
        String id = group.getId();
        if (groupDao.queryGroupByID(id) != null){
            if (hasUserGroup(id)){
                throw new RuntimeException("该群组中存在用户，不可删除该群组");
            }else {
                group.setDelFlag(1);
                int flag = groupDao.updateGroup(group);
                if (flag == 1){
                    return true;
                }else {
                    throw new RuntimeException("删除失败");
                }
            }
        }else {
            throw new RuntimeException("群组不存在");
        }
    }

    @Override
    public List<Answerer> queryAnswererByGroup(String groupId) {
        List<GroupToAnswerer> groupToAnswerers = queryRelationshipByGroupId(groupId);
        List<Answerer> res = new ArrayList<>();
        for (GroupToAnswerer groupToAnswerer : groupToAnswerers){
            Answerer answerer = answererDao.queryAnswererByID(groupToAnswerer.getAnswererId());
            res.add(answerer);
        }
        return res;
    }

    @Override
    public List<GroupToAnswerer> queryRelationshipByGroupId(String groupId) {
        List<GroupToAnswerer> groupToAnswerDaos = groupToAnswererDao.queryRelationshipByGroupId(groupId);
        return groupToAnswerDaos;
    }

    @Transactional
    @Override
    public boolean deleteAnswererFromGroup(String groupId, String answererId) {
        GroupToAnswerer groupToAnswerer = groupToAnswererDao.queryRelation(groupId, answererId);
        if (groupToAnswerer != null){
            int i = groupToAnswererDao.deleteRelation(groupToAnswerer.getId());
            if (i == 1){
                Answerer answerer = answererDao.queryAnswererByID(answererId);
                answerer.setRole(answerer.getRole()-1);
                answererDao.updateAnswerer(answerer);
                return true;
            }else {
                throw new RuntimeException("删除错误,请刷新页面");
            }
        }else {
            throw new RuntimeException("该答者可能已经从该群组删除");
        }
    }
}
